#ifndef TEVENT_H_
#define TEVENT_H_
#define ME(t,a,c,f) new EventBase<t,a>(c, f)
#include "StdAfx.h"
using namespace std;
template<typename A>
class IEventBase
{
public:
	virtual void Invok(A _a) {};
	virtual bool Equal(IEventBase<A>* _ib) = 0;
	virtual ~IEventBase() {}
};
template <typename T, typename A>
class EventBase :public IEventBase<A>
{
	typedef bool(T::* TFunc)(A);
public:
	T* m_cls;
	TFunc m_func;
	EventBase(T* ts, TFunc _func) {
		m_cls = ts;
		m_func = _func;
	}
	virtual void Invok(A _a) {
		(m_cls->*m_func)(_a);
	}
	virtual bool Equal(IEventBase<A>* _ib) {
		EventBase<T, A>* temp = dynamic_cast<EventBase<T, A>*>(_ib);
		if (temp == NULL)
		{
			return false;
		}
		if (temp->m_cls == this->m_cls && temp->m_func == this->m_func)
		{
			return true;
		}
		return false;
	};
};
template<typename A>
class TEvent
{
public:
	vector<IEventBase<A>*>m_evList;
	void operator+=(IEventBase<A>* _ib) {
		for (size_t i = 0; i < m_evList.size(); i++)
		{
			if (m_evList[i]->Equal(_ib)) {
				return;
			}
		}
		m_evList.push_back(_ib);
	}
	void operator-=(IEventBase<A>* _ib) {

		for (size_t i = 0; i < m_evList.size(); i++)
		{
			if (m_evList[i]->Equal(_ib)) {
				delete m_evList[i];
				m_evList.erase(m_evList.begin() + i);
			}
		}
		delete _ib;
	}
	void operator()(A _a) {
		for (size_t i = 0; i < m_evList.size(); i++)
		{
			m_evList[i]->Invok(_a);
		}
	}

};
#endif // !TEVENT_H_
